/**
 * Copyright:Copyright(c)2014-2017
 * Company:厦门市易致达物联网科技有限公司
 * @version 1.0
 */
package shop.controller.front.order;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import shop.core.FrontBaseController;
import shop.core.Services;
import shop.core.common.bean.JSONResult;
import shop.services.front.address.AddressService;
import shop.services.front.address.bean.Address;
import shop.services.front.order.OrderService;
import shop.services.front.order.bean.Order;
import shop.services.front.orderdetail.OrderdetailService;
import shop.services.front.orderdetail.bean.Orderdetail;
import shop.services.manage.system.bean.User;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @类名称：OrderController
 * @创建人：Ltz
 * @创建时间：2017-09-20 下午15:48:44
 * @版本号：1.0
 * @描述：
 */
@Controller
@RequestMapping("/front/order/")
public class OrderController extends FrontBaseController<Order> {
    @Resource
    private OrderService orderService;
    @Resource
    private OrderdetailService orderdetailService;
    @Resource
    private AddressService addressService;
    @Override
    public Services<Order> getService() {
        return orderService;
    }


    /**
     * 跳转到订单列表页面
     *
     * @return
     */
    @RequestMapping("toOrderList")
    public String toOrderList() {return "/front/order/allOrder";}

    /**
     * 跳转到订单详情页面（个人中心入口）
     *
     * @return
     */
    @RequestMapping("toOrderDetail")
    public String toOrderDetail() {return "/front/order/orderDetail";}

    /**
     * 跳转到订单详情页面（购物车入口）
     *
     * @return
     */
    @RequestMapping("toCartOrderDetail")
    public String toCartOrderDetail() {return "/front/order/cartOrderDetail";}

    /**
     * 跳转到订单详情页面（商品详情入口）
     *
     * @return
     */
    @RequestMapping("toProductOrderDetail")
    public String toProductOrderDetail() {return "/front/order/productOrderDetail";}


    /**
     * 查询订单列表
     * @param order
     * @return orderList
     */
    @RequestMapping(value = "selectOrderList",method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectOrderList(HttpServletRequest request,Order order){
        JSONResult jsonResult=new JSONResult();
        User user = (User) request.getSession().getAttribute("userInfo");
        order.setUserID(user.getId());
        List<Order> orderList=orderService.selectList(order);//查询用户订单列表
        for(int i=0;i<orderList.size();i++){//遍历订单列表
            Order rs=orderList.get(i);
            Orderdetail orderdetail=new Orderdetail();
            orderdetail.setOrderID(rs.getId());//赋值
            orderList.get(i).setOrderdetails(orderdetailService.selectOrderDetailList(orderdetail));//查询订单详情列表
        }
        jsonResult.setData(orderList);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 查询订单列表(测试用)
     * @param order
     * @return orderList
     */
    @RequestMapping(value = "selectOrderListCs",method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectOrderListCs(HttpServletRequest request,Order order){
        JSONResult jsonResult=new JSONResult();
//        User user = (User) request.getSession().getAttribute("userInfo");
//        order.setUserID(user.getId());
        List<Order> orderList=orderService.selectList(order);//查询用户订单列表
        for(int i=0;i<orderList.size();i++){//遍历订单列表
            Order rs=orderList.get(i);
            Orderdetail orderdetail=new Orderdetail();
            orderdetail.setOrderID(rs.getId());//赋值
            orderList.get(i).setOrderdetails(orderdetailService.selectOrderDetailList(orderdetail));//查询订单详情列表
        }
        jsonResult.setData(orderList);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    /**
     * 查询订单详情（个人中心入口）
     * @return
     */
    @RequestMapping(value ="selectOrderDetail",method =RequestMethod.GET )
    @ResponseBody
    public JSONResult selectOrderDetail(Orderdetail orderdetail){
        Orderdetail rs=new Orderdetail();
        rs.setOrderID(orderdetail.getOrderID());
        List<Orderdetail> rsList= orderdetailService.selectOrderDetailList(rs);
        return new JSONResult(rsList,true);
    }

    /**
     * 查询用户订单地址
     * @param order
     * @return
     */
    @RequestMapping(value = "selectOrderAddress", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult selectOrderAddress(HttpServletRequest request,Order order) {
        order = orderService.selectOne(order);
        jsonResult = new JSONResult();
        jsonResult.setData(order);
        return jsonResult;
    }

}
